Browser vs NodeJS
JavaScript in the Browser
JavaScript was originally designed to run inside web browsers to make websites interactive. When you open a webpage, the browser downloads the HTML, CSS, and JavaScript, then runs the JavaScript inside the browser engine (like V8 in Chrome, SpiderMonkey in Firefox).
Features in Browser JavaScript
- DOM Manipulation
- Can interact with the Document Object Model (DOM), i.e., change HTML elements, styles, attributes.
- Example: Changing a button’s text or listening for clicks.
- BOM (Browser Object Model)
- Access to
window,navigator,screen,location, etc. - Example: Redirecting users to another page with
window.location.
- Access to
- Limited File Access
- For security, browser JS cannot directly read/write local files (except via
<input type="file">or APIs like IndexedDB).
- For security, browser JS cannot directly read/write local files (except via
- Networking
- Can make HTTP requests using fetch or XMLHttpRequest.
- Example: Fetching API data.
- Security Sandbox
- Runs in a restricted environment (sandbox), preventing access to the operating system directly.
JavaScript in Node.js
Node.js is a runtime environment that allows JavaScript to run outside the browser, built on Google’s V8 engine. It is often used for backend development, file systems, servers, and more.
Features in Node.js
- No DOM/BOM
- Node.js does not have document, window, or alert, because it does not run inside a browser.
- File System Access
- Can read, write, and manipulate files with the fs module.
- Modules (CommonJS/ESM)
- Code is organized into reusable modules using require() or import.
- Networking & Servers
- Can create web servers (using built-in http or frameworks like Express).
- Access to OS
- Can use os, path, process modules for system-level operations.
- Event-driven architecture
- Uses event loop, async I/O, and callbacks extensively.
Differences between Browser JS vs Node.js
| Feature | Browser JS | Node.js |
|---|---|---|
| Environment | Runs in browser sandbox | Runs in server/OS environment |
| DOM/BOM | Available (document, window) | Not available |
| File System | Restricted (no direct access) | Full access via fs module |
| Modules | ES Modules (import/export) | CommonJS (require) + ES Modules |
| Networking | fetch, WebSockets | http, https, net, Express |
| Use Case | Frontend (UI, user interactions) | Backend (servers, APIs, scripts) |
| Security | Runs in sandbox | Access to OS/system resources |